<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="cateList">
              <el-table-column label="序号" width="120" type="index" prop="id" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary">编辑</el-button>
                  <el-button size="small" type="danger" @click="delcatelist(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="q.page"
                :page-sizes="[2, 5, 10, 20]"
                :page-size="q.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="hSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { delRoules, getRoules } from '@/api/settings.js'
export default {
  name: 'VueAdminTemplateSettings',
  data() {
    return {
      q: {
        page: 1,
        pagesize: 2
      },
      cateList: [],
      total: 0
    }
  },
  created() {
    this.loadRoules()
  },
  methods: {
    async loadRoules() {
      const res = await getRoules(this.q)
      // console.log(res)
      this.cateList = res.data.rows
      this.total = res.data.total
    },
    hSizeChange(pagesize) {
      this.q.pagesize = pagesize
      this.loadRoules()
    },
    hCurrentChange(page) {
      this.q.page = page
      this.loadRoules()
    },
    // 删除操作
    delcatelist(id) {
      this.$confirm('你确定要删除吗', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delRoules(id)
          console.log(res)
          // console.log(res)
          this.$message.success(res.message)
          // 重新渲染页面
          this.loadRoules()
        } catch (e) {
          this.$message.error(e.message)
        }
      }).catch(() => {
        // 取消不做什么操作
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
